package com.falcon.service.impl;

import java.util.List;

import com.falcon.domain.dos.ExamQuestionDO;
import com.falcon.service.IExamQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.ExamQuestionPO;
import com.falcon.domain.req.ExamQuestionQuery;
import com.falcon.domain.req.ExamQuestionRequest;

import com.falcon.mapper.IExamQuestionMapper;


@Slf4j
@Service
public class ExamQuestionService implements IExamQuestionService {

    @Autowired
    private IExamQuestionMapper examQuestionMapper;

    @Override
    public Long save(ExamQuestionRequest request){
        ExamQuestionPO examQuestionPO = BeanUtil.copyProperties(request, ExamQuestionPO.class);

        this.examQuestionMapper.insertNotNull(examQuestionPO);

        return examQuestionPO.getId();
    }

    @Override
    public Boolean update(Long id, ExamQuestionRequest request){

        ExamQuestionPO examQuestionPO = BeanUtil.copyProperties(request, ExamQuestionPO.class);
        examQuestionPO.setId(id);

        return this.examQuestionMapper.updateNotNull(examQuestionPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.examQuestionMapper.deleteById(id);
    }

    @Override
    public ExamQuestionDO queryById(Long id){

        ExamQuestionPO examQuestionPO = this.examQuestionMapper.selectById(id);

        ExamQuestionDO examQuestionDO = BeanUtil.copyProperties(examQuestionPO, ExamQuestionDO.class);

        return examQuestionDO;
    }


    @Override
    public List<ExamQuestionDO> queryList(ExamQuestionQuery request){

        Condition condition = request.toCondition();

        List<ExamQuestionPO> poList = this.examQuestionMapper.selectList(condition);

        List<ExamQuestionDO> doList = BeanUtil.copyToList(poList, ExamQuestionDO.class);

        return doList;
    }

    @Override
    public BasePager<ExamQuestionDO> queryPager(ExamQuestionQuery request){


        PageInfo<ExamQuestionPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.examQuestionMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, ExamQuestionDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.examQuestionMapper.deleteByIds(ids);
    }


}

